//	ID : pallab81
// 	PROG : 429
// 	LANG : C++
// 	2011-07-07-13.25.31 Thursday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for( int i = 0; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define mk make_pair
#define pb push_back
#define f first
#define s second
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()

string feed="";
vector< string > dict;
inline void get_dict(){
    dict.clear();
    string line="";
    while( true ){
        getline(cin,line);
        if( line=="*" )break;
        dict.pb( line );
    }
}
inline pair<string,string> split(string &line){
    char buf1[200];
    char buf2[300];
    sscanf(line.c_str(),"%s %s",buf1,buf2);
    //cout<<buf1<<"_"<<buf2<<"\n";
return mk( (string)buf1, (string)buf2 );
}
inline void bfs(string source, string sink){
    queue< pair<string,int> >q;
    map< string, int > vis;

    q.push( mk(source, 0) );
    vis[ source ] = 0;

        while( q.empty()==false ){
            pair<string, int > peak = q.front();
            q.pop();

                fo(i,SZ(dict)){
                    if( LN(dict[i])==LN(peak.f) ){
                        int dif=0;
                        fo(j,LN(dict[i])){
                            dif+= (dict[i][j]!=peak.f[j]);
                        }
                        if(dif==1){
                            if( vis.count( dict[i] ) < 1 ){
                                vis[ dict[i] ] = peak.s+1;
                                q.push( mk(dict[i],peak.s+1) );
                            }
                        }
                    }
                }
        }

        printf("%s %s %d\n",source.c_str(),sink.c_str(), vis[sink] );
}
inline void query(){
    string line;
    while(true){
        getline(cin,line);
        if( LN(line)<1 )break;
        pair<string, string > P = split(line);
        bfs( P.f, P.s );
    }
}
inline void doit(){
    get_dict();
    query();
}

int main(){
    int cases;
    scanf("%d\n",&cases);
    while(cases--){
        printf("%s",feed.c_str());
        doit();
        feed="\n";
    }

return 0;
}

